package cn.com.bluemoon.daps.sync.core.sync.sql.mysql;

import cn.com.bluemoon.daps.common.datamarking.IDataMarking;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.system.entity.DapSystemDatasourceEnvironment;
import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Jarod.Kong
 * @date 2021/6/28 14:18
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class MysqlConnect implements IDataMarking {
    private String scriptPath = "/usr/bin/mysql";
    private String user, password, database, host;
    private Integer port;
    private DatabaseType dbType;
    {
        dbType = DatabaseType.MYSQL;
    }

    public MysqlConnect(DapSystemDatasourceEnvironment ds) {
        this.dbType = ds.getDbType();
        this.user = ds.getUsername();
        this.password = ds.decryptPassword();
        this.database = ds.getDatabaseName();
        this.host = ds.getHost();
        this.port = ds.getPort();
    }

    public String getDbTypeName() {
        if (dbType == null) dbType = DatabaseType.MYSQL;
        return dbType.getDbName();
    }

    @Override
    public String toString() {
        return toString(false);
    }

    @Override
    public String toString(boolean dataMark) {
        if (dbType == null) dbType = DatabaseType.MYSQL;
        String pw = dataMark ? StrUtil.repeat("*", 8) : password;
        return "MysqlConnect{" +
                "scriptPath='" + scriptPath + '\'' +
                ", user='" + user + '\'' +
                ", password='" + pw + '\'' +
                ", database='" + database + '\'' +
                ", host='" + host + '\'' +
                ", port=" + port +
                ", dbType=" + dbType.getDbName() +
                '}';
    }


}